package com.andaily.domain.dto.log;

import com.andaily.domain.shared.grouper.GroupResult;
import com.andaily.domain.shared.grouper.impl.LogDtoDateGrouper;
import com.andaily.domain.shared.paginated.DefaultPaginated;
import com.andaily.domain.shared.security.SecurityUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Shengzhao Li
 */
public class LogOverviewDto extends DefaultPaginated<LogDto> {

    private String previousLastDate;
    private boolean showMore;

    public LogOverviewDto() {
    }

    /**
     * 对查询的数据按照 日期进行分组
     *
     * @return GroupResult list
     */
    public List<GroupResult<String, LogDto>> getGroupResults() {
        LogDtoDateGrouper grouper = new LogDtoDateGrouper(getList());
        return grouper.group().getGroupResults();
    }

    public void afterLoad() {
        this.showMore = isHasNext();
    }

    /**
     * 判断当页数据开始显示时是否显示日期
     *
     * @return True is show, otherwise false
     */
    public boolean isShowLogDate() {
        return !(StringUtils.isNotEmpty(previousLastDate)
                && !list.isEmpty()
                && previousLastDate.equals(list.get(0).getLogDate()));
    }

    public boolean isShowMore() {
        return showMore;
    }

    public void setShowMore(boolean showMore) {
        this.showMore = showMore;
    }

    public Map<String, Object> params() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userGuid", SecurityUtils.currentUserGuid());
        map.put("perPageSize", getPerPageSize());
        map.put("startIndex", getStartIndex());
        return map;
    }

    public String getPreviousLastDate() {
        return previousLastDate;
    }

    public void setPreviousLastDate(String previousLastDate) {
        this.previousLastDate = previousLastDate;
    }
}
